"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lei.chang on '23/04/2023'
    comment: 服务流程相关
"""
from sqlalchemy import func
from sqlalchemy.dialects.mysql import VARCHAR, INTEGER, DATETIME, TIMESTAMP, BOOLEAN, TEXT

from project.core.base import CustomModel, CustomList, CustomDict
from project.core.base import Column


class Knowledge(CustomModel):
    """ 知识库相关
    """
    __tablename__ = "knowledge"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    code = Column(VARCHAR(30), nullable=False, server_default='', comment='知识库编号')
    title = Column(VARCHAR(30), nullable=False, server_default='', comment='标题')
    sub_title = Column(VARCHAR(30), nullable=False, server_default='', comment='副标题')
    image = Column(VARCHAR(200), nullable=False, server_default='', comment='知识库图片')
    organization_code = Column(VARCHAR(20), nullable=False, server_default='', comment='机构编号', is_displayed=False)
    bind_agent_count = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='关联agent数量')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)


class KnowledgeData(CustomModel):
    """ 知识库数据集表
    """
    __tablename__ = "knowledge_data"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    title = Column(VARCHAR(200), nullable=False, server_default='', comment='数据名称')
    file_name = Column(VARCHAR(200), nullable=False, server_default='', comment='文件名称')
    text = Column(TEXT, nullable=False, server_default='init', comment='数据文本')
    text_split = Column(CustomList, nullable=False, server_default='', comment='数据文本(切分后)')
    status = Column(VARCHAR(30), nullable=False, server_default='init', comment='状态 init(初始化)/pdf_converting(pdf文件转化中)/md_converting(md文件转化中)/embedding(向量化中)/success(已完成)')
    knowledge_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='知识库id')

    import_mode = Column(VARCHAR(30), nullable=False, server_default='', comment='导入模式: image_text(图文)/qa(QA对)/text(文本)')
    task_id = Column(VARCHAR(30), nullable=False, server_default='', comment='文件导入任务id')
    file_url = Column(VARCHAR(500), nullable=False, server_default='', comment='上传的文件链接(url)')
    md_url = Column(VARCHAR(500), nullable=False, server_default='', comment='最终md文件链接(url)')

    feishu_url = Column(VARCHAR(500), nullable=False, server_default='', comment='飞书云文档链接')
    flush_frequency = Column(VARCHAR(30), nullable=False, server_default='', comment='更新频率 minute(按分钟)/hour(按小时)/day(按天)/no_auto(不自动更新)')
    flush_frequency_value = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='更新频率值')
    import_type = Column(VARCHAR(30), nullable=False, server_default='local_file', comment='导入类型 local_file(本地文件)/feishu_url(飞书链接)')
    flush_start_time = Column(DATETIME, nullable=True, comment='刷新开始时间')
    flush_end_time = Column(DATETIME, nullable=True, comment='刷新结束时间')
    fail_message = Column(VARCHAR(500), nullable=False, server_default='', comment='失败信息')

    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now())
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)

    async def do_status_transfer(self, session, status):
        """ 状态
        :param session:
        :param status:
        :return:
        """
        self.status = status

        await session.commit()


class KnowledgeDetail(CustomModel):
    """ 知识库详情
    """
    __tablename__ = "knowledge_detail"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    code = Column(VARCHAR(100), comment='知识库详情编号')
    # question = Column(VARCHAR(500), nullable=False, server_default='', comment='问题')
    # answer = Column(TEXT, nullable=False, server_default='', comment='答案')
    text = Column(TEXT, nullable=False, server_default='', comment='文本')
    knowledge_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='知识库id')
    knowledge_data_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='知识库数据集id')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)


class KnowledgeDataImportLog(CustomModel):
    """ 知识库文件导入日志表
    """
    __tablename__ = "knowledge_data_import_log"
    id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
    task_id = Column(VARCHAR(30), nullable=False, server_default='', comment='文件导入任务id')
    knowledge_data_id = Column(INTEGER(unsigned=True), nullable=False, server_default='0', comment='知识库数据集id')
    status = Column(VARCHAR(30), nullable=False, server_default='init', comment='状态 init(初始化)/pdf_converting(pdf文件转化中)/md_converting(md文件转化中)/embedding(向量化中)/success(已完成)')
    callback_text = Column(CustomDict, nullable=False, server_default='', comment='回调内容')
    is_deleted = Column(BOOLEAN, nullable=False, server_default='0', comment='是否删除 0: 未删除 1: 已删除', is_displayed=False)
    create_time = Column(DATETIME, nullable=False, server_default=func.now(), is_displayed=False)
    update_time = Column(TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now(), is_displayed=False)
